package com.lwz.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwz.eduservice.client.UcenterClient;
import com.lwz.eduservice.entity.EduComment;
import com.lwz.eduservice.service.EduCommentService;
import com.lwz.util.JwtInfo;
import com.lwz.util.JwtUtil;
import com.lwz.util.R;
import com.lwz.util.orderVo.UcenterMemberOrder;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Lw中
 * @since 2020-11-19
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class CommentFrontController {

    @Autowired
    private EduCommentService eduCommentService;

    /** 注入ucenterClient用于验证用户是否已登录和获取用户的信息 */
    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 分页查询所有用户对课程的评论
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("/getCommentFrontList/{current}/{limit}")
    public R getCommentFrontList(@PathVariable long current, @PathVariable long limit,
                                 @ApiParam(name = "courseQuery", required = false) String courseId) {
        Page<EduComment> commentPage = new Page<>(current, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseId)) {
            wrapper.eq("course_id", courseId);
        }
        // 将分页数据全部存储在commentPage中
        eduCommentService.page(commentPage, wrapper);
        // 从commentPage中获取评论信息
        List<EduComment> records = commentPage.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", commentPage.getCurrent());
        map.put("pages", commentPage.getPages());
        map.put("size", commentPage.getSize());
        map.put("total", commentPage.getTotal());
        map.put("hasNext", commentPage.hasNext());
        map.put("hasPrevious", commentPage.hasPrevious());
        return R.ok().data(map);
    }

    /**
     * LWZ TODO : 2020/11/20 添加评论接口
     */

    /**
     * 添加评论
     * @param httpRequest
     * @param eduComment
     * @return
     */
    @PostMapping("/addComment")
    public R addComment(HttpServletRequest httpRequest, @RequestBody EduComment eduComment) {
        JwtInfo jwtToken = JwtUtil.getMemberIdByJwtToken(httpRequest);
        if(StringUtils.isEmpty(jwtToken)) {
            return R.error().code(20001).message("请登录");
        }
        eduComment.setMemberId(jwtToken.getId());

        UcenterMemberOrder ucenterInfo = ucenterClient.getUserInfo(jwtToken.getId());

        eduComment.setNickname(ucenterInfo.getNickname());
        eduComment.setAvatar(ucenterInfo.getAvatar());

        eduCommentService.save(eduComment);
        return R.ok();
    }

}

